//
//  MAPTableViewController.m
//  Kjs-iOS
//
//  Created by Pat on 16/8/5.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "MAPTableViewController.h"

@interface MAPTableViewController ()

@end

@implementation MAPTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];    
    self.tableView.backgroundColor = [UIColor colorWithRed:245.0/255 green:245.0/255 blue:245.0/255 alpha:1.0];
    
    //cell 高度自适应要求指定估计高度
    self.tableView.estimatedRowHeight = 44.0f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - initialize
- (instancetype)initWithStoryboardName:(NSString *)storyboardName ViewControllerIdentifier:(NSString *)viewControllerIdentifier {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    self = [storyboard instantiateViewControllerWithIdentifier:viewControllerIdentifier];
    return self;
}
- (instancetype)initWithViewControllerIdentifier:(NSString *)viewControllerIdentifier {
    return [self initWithStoryboardName:@"Main" ViewControllerIdentifier:viewControllerIdentifier];
}

#pragma mark - display method
- (void)displayViewController:(UIViewController *)viewController {
    if(self.navigationController != nil) {
        [self.navigationController pushViewController:viewController animated:YES];
    }
    else {
        [self presentViewController:viewController animated:YES completion:nil];
    }
}

/**
 只要navigationController中的任意一层的ViewController调用
 [viewControllerInstance dismissViewControllerAnimated:YES completion:nil];
 则整个navigationController消失
 */
- (void)dismiss:(BOOL)animated {
    animated = YES;
    if([self.navigationController.viewControllers count] > 1) {
        [self.navigationController popViewControllerAnimated:animated];
    }
    else {
        [self dismissViewControllerAnimated:animated completion:nil];
    }
}

#pragma mark - touch event
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
#pragma mark - life cycle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = !self.naviTopView;
    if(!self.naviTopView) {
        UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss:)];
        leftBarButton.tintColor = [UIColor whiteColor];
        self.navigationItem.leftBarButtonItem = leftBarButton;
    }
}

#pragma mark - tableview style config
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    //去除 cell 之间分割线左侧的空白
    cell.separatorInset = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsZero;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //cell 高度自适应
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    //让 footer 透明即颜色等同于 tableview 背景色
    view.tintColor = [UIColor clearColor];
}

@end
